public class Solution404 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int ans=0;

    public int sumOfLeftLeaves(TreeNode root) {
        dfs(root,false);
        return ans;
    }

    private void dfs(TreeNode root, boolean b) {
        if (root==null){
            return;
        }
        if (b && root.left==null && root.right==null){
            ans=ans+root.val;
            return;
        }
        dfs(root.left,true);
        dfs(root.right,false);
    }

    public static void main(String[] args) {
        System.out.println(new Solution404().sumOfLeftLeaves(new TreeNode(3,new TreeNode(9),new TreeNode(20,new TreeNode(15),new TreeNode(7)))));
    }
}
